#include "test_jsoncmd.h"
#include "slm_cmd_json.h"
#include "at_slm332.h"


cJSON* create_json_ty(void) {
    cJSON* root = cJSON_CreateObject();
    cJSON_AddStringToObject(root, "cmd", "mg_cfg_get");
    cJSON_AddStringToObject(root, "method", "get");
    cJSON* arg = cJSON_CreateObject();
    cJSON_AddNumberToObject(arg, "index", 0);
    cJSON_AddNumberToObject(arg, "count", 5);
    cJSON_AddItemToObject(root, "arg", arg);
    return root;
}


cJSON* create_json_slm4g(void) {
    cJSON* root = cJSON_CreateObject();
    cJSON_AddStringToObject(root, "cmd", "mg_4g_get");
    cJSON_AddStringToObject(root, "method", "get");
    cJSON* arg = cJSON_CreateObject();
    cJSON_AddNumberToObject(arg, "index", 0);
    cJSON_AddNumberToObject(arg, "count", 5);
    cJSON_AddItemToObject(root, "arg", arg);
    return root;
}


cJSON* create_json_smqtt(void){
    cJSON* root = cJSON_CreateObject(); //主节点
    cJSON_AddStringToObject(root, "cmd", "mg_cfg_set");
    cJSON_AddStringToObject(root, "method", "set");
    cJSON* arg = cJSON_CreateObject();//子节点
    cJSON_AddStringToObject(arg, "ip_host", "192.168.0.22");
    cJSON_AddNumberToObject(arg, "port", 1919);
    cJSON_AddStringToObject(arg, "clinet_id", "ppja");
    cJSON_AddStringToObject(arg, "user_name", "ss");
    cJSON_AddStringToObject(arg, "pass_word", "aa");
    cJSON_AddStringToObject(arg, "subtopic", "/138/D19LIJwwqevn/get");
    cJSON_AddStringToObject(arg, "pubtopic", "/138/Dcctyjbjkerty/post");
    cJSON_AddItemToObject(root, "arg", arg);
    return root;
}

char ww[1024];
char replay[1024];
///测试代码
int v_test(void) {
    
		cJSON* node = create_json_ty();
		cJSON_PrintPreallocated(node, (char*)ww, 1024, 0);
		cJSON_Delete(node);
    // 测试 get 命令
    char cmd_get[] = "{\"method\":\"get\",\"cmd\":\"mg_cfg_get\"}";
    slm_json_cmd_switch(ww, replay);
    printf("Replay for get: %s\n", replay);
	
	  // 测试 get_4g 命令
	  cJSON* node2=create_json_slm4g();
	  cJSON_PrintPreallocated(node2, (char*)ww, 1024, 0);
    slm_json_cmd_switch(ww, replay);
    printf("Replay for set: %s\n", replay);
	  cJSON_Delete(node2);
    // 测试 get 命令
    char cmd_set[] = "{\"method\":\"get\",\"cmd\":\"mg_online_get\"}";
    slm_json_cmd_switch(cmd_set, replay);
    printf("Replay for set: %s\n", replay);
		cJSON* node1 = create_json_smqtt();
		cJSON_PrintPreallocated(node1, (char*)ww, 1024, 0);
		cJSON_Delete(node1);
//    // 测试 set 命令
    char cmd_other[] = "{\"method\":\"set\"}";
    slm_json_cmd_switch(ww, replay);
    printf("Replay for other: %s\n", replay);
	
	 // 测试 mg_cfg_set 命令
    char cmd_mg_cfg_set[] = "{\"cmd\":\"mg_cfg_set\",\"method\":\"set\",\"arg\":{\"ip_host\":\"192.168.0.2\",\"port\":1833,\"Qos\":3}}";
    slm_json_cmd_switch(cmd_mg_cfg_set, replay);
    printf("Replay for mg_cfg_set: %s\n", replay);

    // 测试未知命令
    char cmd_unknown[] = "{\"method\":\"unknown\"}";
    slm_json_cmd_switch(cmd_unknown, replay);
    printf("Replay for unknown: %s\n", replay);

    // 测试无效 JSON
    char cmd_invalid_json[] = "{\"method\":\"get\"";
    slm_json_cmd_switch(cmd_invalid_json, replay);
    printf("Replay for invalid JSON: %s\n", replay);

    // 测试无效 method
    char cmd_invalid_method[] = "{\"method\":123}";
    slm_json_cmd_switch(cmd_invalid_method, replay);
    printf("Replay for invalid method: %s\n", replay);

    return 0;
}